﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using crm_library.DataAccess.TableReps;
using crm_library.DataAccess.ObjectReps;
using crm_core.List;
using crm_library.DataAccess;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;

namespace crm_dal.Objects
{
	[DataContractAttribute]
	public class Followup : ObjectRepresentation
	{
		#region constructor
		public new const string TableName = "Followup";
		public new const string ObjectName = "Followup";

		public Followup(int followupID)
			: base(TableName, ObjectName)
		{
			FollowupID = followupID;
			Load();
		}

		public Followup()
			: base(TableName, ObjectName)
		{

		}
		#endregion constructor

		#region properties
		[ListingFieldAttribute("Followup ID")]
		[DataFieldAttribute("FollowupID", false, true)]
		[DataMember]
		public int FollowupID { get; set; }

		[DataFieldAttribute("UserID", false)]
		[DataMember]
		public int UserID { get; set; }
		private User _userObj;
		public User UserObj
		{
			get
			{
				if (_userObj == null) _userObj = new User(UserID);
				return _userObj;
			}
		}

		[DataFieldAttribute("ContactID", false)]
		[DataMember]
		public int ContactID { get; set; }
		private Contact _contactObj;
		public Contact ContactObj
		{
			get
			{
				if (_contactObj == null) _contactObj = new Contact(ContactID);
				return _contactObj;
			}
		}

		[DataFieldAttribute("FollowupByDate", true)]
		[DataMember]
		public DateTime FollowupByDate { get; set; }

		[DataFieldAttribute("DateFollowedUpOn", false)]
		[DataMember]
		public DateTime DateFollowedUpOn { get; set; }

		[DataFieldAttribute("ResponseType", false)]
		[DataMember]
		public int ResponseTypeInt { get; set; }
		public ContactTypes ResponseTypeEnum
		{
			get { return (ContactTypes)ResponseTypeInt; }
			set { ResponseTypeInt = (int)value; }
		}
		[ListingFieldAttribute("Response Type")]
		public string ResponseTypeStr
		{
			get
			{
				return ResponseTypeEnum.ToDisplayString();
			}
		}

		[DataFieldAttribute("Notes", true)]
		[DataMember]
		public string Notes { get; set; }


		public bool FollowUpWasNeeded
		{
			get
			{
				return (FollowupByDate != null);
			}
		}
		[ListingFieldAttribute("Needs Followup")]
		public string FollowUpWasNeededString
		{
			get{
				return FollowUpWasNeeded ? "Yes" : "No";
			}
		}

		public string ShortDescription
		{
			get
			{
				const string format = "{0} on {1}, by {2}. {3}";
				return String.Format(format, ContactObj.ClientName, DateFollowedUpOn, UserObj.LoginName, FollowUpWasNeededString);
			}
		}

		#endregion properties

		#region ObjectRepresentation implementations
		public override int GetPKValue()
		{
			return FollowupID;
		}

		public override void SetPKValue(int pk)
		{
			FollowupID = pk;
		}

		public override int GetNextPKValue()
		{
			int maxInt = 0;

			foreach (var item in DB.ReadObjectList(new Followup()))
			{
				maxInt = Math.Max(maxInt, item.GetPKValue());
			}
			return maxInt + 1;
		}

		public override void Load(int pk)
		{
			DB.ReadObject(this, pk);
		}

		public override void Update()
		{
			DB.UpdateObject(this, GetPKValue());
		}

		public override void Delete(int pk)
		{
			DB.DeleteObject(this, pk);
		}

		public override void Create()
		{
			DB.CreateObject(this);
		}

		public override ListItem ToListItem()
		{
			return new ListItem(ShortDescription, GetPKValue().ToString());
		}

		#endregion ObjectRepresentation implementations

		public static List<Followup> GetAll()
		{
			return DB.ReadObjectList(new Followup());
		}
	}
}
